package com.company;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @date : 2023-03-23 下午8:07
 *
 */

@Slf4j
public class P050_CopyFileWithFileChannel {

    public static void main(String[] args) {
        // try {
        //     Thread.sleep(30000);
        // } catch (Exception e) {
        //     e.printStackTrace();
        // }
        String fileName = "file/new.txt";
        String copyFile = "file/copy.txt";
        long start = System.nanoTime();
        copyNio(fileName, copyFile);
        long time = System.nanoTime() - start;
        log.info("读取文件数据耗时：{} s", time / 1000.0 / 1000 / 1000);
    }


    public static void copyNio(String from, String to) {
        try (FileChannel input = new FileInputStream(new File(from)).getChannel();
             FileChannel output = new FileOutputStream(new File(to)).getChannel()) {
            output.transferFrom(input, 0, input.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
